1 module engine_getter;
2 import commons;
3 
4 
5 private immutable requiredFiles = ["dub.json", "hipreme_engine.sln", "HipremeEngine.code-workspace"];
6 
7 private bool isValidEnginePath(string path)
8 {
9     return filesExists(path, requiredFiles);
10 }
11 private bool isRunningFromDubRun(string enginePath)
12 {
13     return executeShell("cd \""~enginePath~"\" && git status").status != 0;
14 }
15 
16 bool setupEngine(ref Terminal t, ref RealTimeConsoleInput input)
17 {
18     if(!("hipremeEnginePath" in configs))
19     {
20 	    import std.array:replace;
21         string hipremeEnginePath;
22         if("HIPREME_ENGINE" in environment)
23         {
24             hipremeEnginePath = environment["HIPREME_ENGINE"];
25             if(hipremeEnginePath[0] == '"' && hipremeEnginePath[$-1] == '"') 
26                 hipremeEnginePath = hipremeEnginePath[1..$-1];
27             t.writelnHighlighted("Using existing environment variable 'HIPREME_ENGINE' for hipremeEnginePath");
28         }
29         else 
30         {
31             ///Check for existing Hipreme Engine
32             import core.runtime;
33             string[] directories = 
34             [
35                 ".", 
36                 buildNormalizedPath(std.file.getcwd(), "..", "..", ".."), 
37                 dirName(Runtime.args[0]),
38                 buildNormalizedPath(dirName(Runtime.args[0]), "..", "..", ".."),
39             ];
40             foreach(dir; directories)
41             {
42                 if(isValidEnginePath(dir))
43                 {
44                     hipremeEnginePath = dir;
45                     break;
46                 }
47             }
48             if(hipremeEnginePath.length)
49             {
50                 if(isRunningFromDubRun(hipremeEnginePath))
51                 {
52                     t.writelnHighlighted(
53                         "HipremeEngine is present, but dub does not support git repositories containing submodules.\n",
54                         "\tHipremeEngine needs to be cloned.");
55                     goto clone;
56                 }
57                 t.writelnHighlighted("Using engine path found at directory '"~hipremeEnginePath~"'");
58             }
59             else
60             {
61                 clone: bool canClone = pollForExecutionPermission(t, input, "HipremeEngine path wasn't found in configs. Do you want to clone the engine?");
62                 if(canClone)
63                 {
64                     if(!hasGit)
65                     {
66                         if(!installGit(t, input))
67                         {
68                             t.writelnError("Could not install Git.");
69                             return false;
70                         }
71                     }
72                     if(wait(spawnShell(getGitExec~" clone "~hipremeEngineRepo)) != 0)
73                     {
74                         t.writelnError("Could not clone HipremeEngine on repo ", hipremeEngineRepo);
75                         return false;
76                     }
77                     hipremeEnginePath = buildNormalizedPath(std.file.getcwd(), "HipremeEngine");
78                 }
79                 else 
80                 {
81                     hipremeEnginePath = getValidPath(t, "HipremeEngine Path: ");
82                     if(!isValidEnginePath(hipremeEnginePath))
83                     {
84                         import std.string:join;
85                         t.writelnHighlighted("Path is not valid. HipremeEngine path should have: \n\t"~requiredFiles.join("\n\t"));
86                         goto clone;
87                     }
88                 }
89             }
90         }
91         configs["hipremeEnginePath"] = hipremeEnginePath;
92         updateConfigFile();
93         loadSubmodules(t, input);
94     }
95 	environment["HIPREME_ENGINE"] = configs["hipremeEnginePath"].str;
96     return true;
97 }